#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>


int main()
{

  int fds[2];
  int pip = pipe(fds);
  printf("fds[0]:%d fds[1]:%d\n",fds[0],fds[1]);
  if(pip < 0)
  {
    perror("pipe is error");
  }
  if(fork() == 0)
  {
    //child write
    close(fds[0]);
    int i = 0;
    int size = 1;
    const char* buff[2] = {"hello\0","world\0"};
    while(size > 0)
    {
      size = write(fds[1],buff[i],strlen(buff[i]));
      printf("%d\n",i++);
      sleep(1);
      //提前关闭
    // close(fds[1]);

      // break;
    }
    close(fds[1]);
    //if read close , wirte peocesses is exit ,you don't see printf;
    //  printf("parent is eixt\n");
      return 0;
  }
  //parent read
  close(fds[1]);
  int size = 1;
  while(size > 0)
  {
    char buff[1024*1024*4];
    size = read(fds[0],buff,sizeof(buff)-1);
    buff[size] = 0;
    printf("chile write:%s\n",buff);
    //break;
  }
  waitpid(-1,NULL,0);
  printf("child write is close\n");
  close(fds[0]);
  return 0;
}
